package com.woniu.uums.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.dto.RegisterDto;
import com.woniu.common.dto.ZyManageDto;
import com.woniu.common.utils.ResponseData;
import com.woniu.common.utils.ResponseEnum;
import com.woniu.uums.pojo.ZyManage;
import com.woniu.uums.service.ZyManageService;
import com.woniu.uums.mapper.ZyManageMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.woniu.common.constant.FinanceConstant.JWT.TOKEN_HEADER;
import static com.woniu.common.constant.FinanceConstant.Salt.SALT;

/**
 * @author Administrator
 * @description 针对表【zy_manage】的数据库操作Service实现
 * @createDate 2024-08-05 22:40:15
 */
@Service
public class ZyManageServiceImpl extends ServiceImpl<ZyManageMapper, ZyManage>
        implements ZyManageService {
    @Value("${snowflake.workerId}")
    private Long workerId;
    @Value("${snowflake.datacenterId}")
    private Long datacenterId;

    @Override
    public ResponseData<?> login(RegisterDto registerDto) {
        String username = registerDto.getManageAccount();
        String password = registerDto.getManagePwd();
        if (StringUtils.isEmpty(registerDto.getManageAccount())) {
            return new ResponseData<>().fail(ResponseEnum.USER_NULL);
        }
        if (StringUtils.isEmpty(registerDto.getManagePwd())) {
            return new ResponseData<>().fail(ResponseEnum.PWD_NULL);
        }
        //加盐
        password = SecureUtil.md5(password + SALT);
        //查询账号密码是否匹配
        ZyManage zyManage = lambdaQuery().eq(ZyManage::getManageAccount, username)
                .eq(ZyManage::getManagePwd, password)
                .one();
        if (ObjectUtils.isEmpty(zyManage)) {
            return new ResponseData<>().fail(ResponseEnum.LOGIN_ERROR);
        }
        //查询出用户消息则生成JWT，并返回
        Map<String, Object> map = new HashMap<>(1);
        ZyManageDto zyManageDto = new ZyManageDto();
        BeanUtil.copyProperties(zyManage, zyManageDto);
        map.put("zyManage", zyManageDto);
        String token = JWTUtil.createToken(map, TOKEN_HEADER.getBytes());
        return new ResponseData<>().success(token);
    }

    @Override
    public ResponseData<?> register(RegisterDto registerDto) {
        if (StringUtils.isEmpty(registerDto.getManageAccount())) {
            return new ResponseData<>().fail(ResponseEnum.ACCOUNT_NULL);
        }
        if (StringUtils.isEmpty(registerDto.getManagePwd())) {
            return new ResponseData<>().fail(ResponseEnum.PWD_NULL);
        }
        registerDto.setManagePwd(SecureUtil.md5(registerDto.getManagePwd() + SALT));
        registerDto.setId(String.valueOf(IdUtil.getSnowflake(workerId, datacenterId).nextId()));
        if (save(BeanUtil.toBean(registerDto, ZyManage.class))) {
            return new ResponseData<>().success();
        }
        return new ResponseData<>().fail(ResponseEnum.FAIL);
    }
}




